package net.zjitc.controller;

import net.zjitc.domain.Category;
import net.zjitc.domain.CategorySecond;
import net.zjitc.domain.Product;
import net.zjitc.service.CategoryService;
import net.zjitc.service.ProductService;
import net.zjitc.service.SecondService;
import net.zjitc.service.impl.CategoryServiceImpl;
import net.zjitc.service.impl.ProductServiceImpl;
import net.zjitc.service.impl.SecondServiceImpl;
import net.zjitc.utils.PageBean;
import net.zjitc.utils.URLMapping;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 2 * @Author: 洪瑶
 * 3 * @Date: 2018/11/21 15:50
 * 4
 */
public class ProductController {
    CategoryService cservice = new CategoryServiceImpl();
    ProductService pservice = new ProductServiceImpl();
    SecondService secondService = new SecondServiceImpl();
    @URLMapping(url = "/findByCid")
    public String findByCid(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String cid = request.getParameter("cid");
        String page1 = request.getParameter("page");
        int id = Integer.parseInt(cid);
        int page = Integer.parseInt(page1);
        List<Product> findbymore = pservice.findCategoryPageList(id, page, 12);
        pagebean(request,page,findbymore,12);
        request.setAttribute("cid",cid);
        cateforeahsecond(request);
        return "/WEB-INF/user/productList.jsp";
    }


    @URLMapping(url = "/findByCsid")
    public String findByCsid(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        int csid = Integer.parseInt(request.getParameter("csid"));
        int page = Integer.parseInt(request.getParameter("page"));
        List<Product> findbymore = pservice.findCategorySecondPageList(csid, page, 12);
        pagebean(request,page,findbymore,12);
        request.setAttribute("csid",csid);
        cateforeahsecond(request);
        return "/WEB-INF/user/productList.jsp";

    }

    @URLMapping(url = "/product_findByPid")
    public String product_findByPid(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {
        bypid(request);
        return "/WEB-INF/user/product.jsp";
    }

    @URLMapping(url = "/findByPid")
    public String findByPid(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {
        bypid(request);
        return "/WEB-INF/user/product.jsp";
    }

    public void cateforeahsecond(HttpServletRequest request) throws SQLException {
        List<Category> call = cservice.findAll();
        List<CategorySecond> csall = secondService.findAll();
        for (Category c :call
                ) {
            List<CategorySecond> list = new ArrayList<>();
            for (CategorySecond s:csall
                    ) {
                if (c.getCid()==s.getCid())
                {
                    list.add(s);
                }
            }
            c.setCategorySeconds(list);
        }
        request.setAttribute("clist",call);
    }

    public void bypid(HttpServletRequest request) throws SQLException {
        ProductService pservice = new ProductServiceImpl();
        String id = request.getParameter("pid");
        int pid = Integer.parseInt(id);
        Product product = null;
        product = pservice.findbyid(pid);
        request.setAttribute("p",product);
        cateforeahsecond(request);
    }

    public void pagebean(HttpServletRequest request,int page,List<Product>findbymore, int pageSize){
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);
        pageBean.setPage(page);
        pageBean.setCount(pservice.findcount());
        pageBean.setPageList(findbymore);
        pageBean.setPageCount(pageBean.getPageCount());
        request.setAttribute("page",pageBean);
    }
}
